class Vec2 {

	public x: number = 0;
	public y: number = 0;

	public constructor(x: number, y: number) {
		this.x = x;
		this.y = y;
	}

	public add(v: Vec2): Vec2 {
		return new Vec2(this.x + v.x, this.y + v.y);
	}

	public substract(v: Vec2): Vec2 {
		return new Vec2(this.x - v.x, this.y - v.y);
	}

	public dotProduct(v: Vec2): number {
		return this.x * v.x + this.y + v.y;
	}

	// 获取单位向量（即向量大小为1，用于表示向量方向），一个非零向量除以它的模即可得到单位向量
	public normalize() {
		var v = new Vec2(0, 0)
		var m = this.getLength()
		if (m !== 0) {
			v.x = this.x / m
			v.y = this.y / m
		}
		return v
	}

	// 获取当前向量的法向量（垂直）
	public perpendicular() {
		var v = new Vec2(0, 0)
		v.x = this.y
		v.y = 0 - this.x
		return v;
	}

	// 获取边缘法向量的单位向量，即投影轴
	public normal() {
		var p = this.perpendicular()
		return p.normalize()
	}

	public distance(v: Vec2): number {
		var dx = this.x - v.x,
			dy = this.y - v.y;
		return Math.sqrt(dx * dx + dy * dy);
	}

	public angle(p: Vec2): number {
		return Math.atan2(p.x - this.x, p.y - this.y);
	}

	public addAngle(a: number, s: number): void {
		this.x += Math.sin(a) * s;
		this.y += Math.cos(a) * s;
	}

	public rotate(a: number): void {
		var px: number = this.x;
		this.x = this.x * Math.cos(a) - this.y * Math.sin(a);
		this.y = px * Math.sin(a) + this.y * Math.cos(a);
	}

	public set xy(v: Vec2) {
		this.x = v.x;
		this.y = v.y;
	}

	public get way(): number {
		return Math.atan2(this.x, this.y);
	}

	public getLength(): number {
		return Math.sqrt(this.x * this.x + this.y * this.y);
	}
}